package com.hnkywl.sts.action.jsgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.jsgl.WljsdDao;
import com.hnkywl.sts.dao.jsgl.WljsdmxDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.base.DxpzService;
import com.hnkywl.sts.service.jsgl.WljsdService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-2
 * Time: 下午4:14
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/jsgl/wljsd")
public class WljsdAction extends SimpleActionSupport {
    private WljsdService wljsdService;
    private DxpzService dxpzService;
    private WljsdDao wljsdDao;
    private WljsdmxDao wljsdmxDao;
    //操作类型 add表示新增,update表示修改
    private String operation;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //传入的企业机构Id值
    private Integer type;
    //结算单号
    private String jsdh;
    //客户的ID
    private Long glqyId;
    //发票类型ID
    private Long fpzlId;
    //客户的名称
    private String glqyQc;
    //公司的名称
    private String gsJc;
    //申请（查询）开始时间
    private Date sqrqStart;
    //申请（查询）结束时间
    private Date sqrqEnd;
    //物流结算单实体
    private Jsd wljsd;
    //物流结算单的ID
    private Long wljsdId;
    //物流结算单状态
    private Integer wljsdZt;
    //申报人的Id
    private Long sprId;
    //申报人所在公司的Id
    private Long gsId;
    //状态
    private Integer zt;
    //物流商Id
    private Long sydwId;
    //关联单号
    private String gldh;
    //公司Id
    private Long sydwGsId;
    //物流商名称
    private String sydwqc;
    private Date CurrentTime = new Date();
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    //物流结算单的查询分页显示
    @Action("wljsdList")
    public void wljsdList() {
        this.renderJson(this.wljsdService.findAllWljsd(zt, jsdh, sqrqStart, sqrqEnd, glqyId, glqyQc, gsId, gsJc, fpzlId, page, rows));
    }

    @Action("wljsd")
    public String wljsd(){
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //物流费用明细
    @Action("wldSearchList")
    public void glfySearchDetail() {
        this.renderJson(this.wljsdService.findWldSearch(sydwGsId,sydwId,sydwqc,gldh,wljsdId,wljsdZt));
    }

    @Action("wldDetailList")
    public void wljsdDetail() {
        this.renderJson(this.wljsdService.findwldDetail(wljsdId));
    }

    @Actions({@Action("wljsd_content"),@Action("wljsd_detail")})
    public String wljsdContent() {
        sprId = YgService.getCurrentYg().getId();
        gsId = YgService.getCurrentYgZgGsId();
        //进入update页面
        if (wljsdId != null) {
            wljsd = wljsdService.findWljsdDetail(wljsdId);
        } else {
            wljsd = new Jsd();
        }
        return SUCCESS;
    }

    /**
     * 方法名:     addWljsd
     * 功能描述:      增加物流结算单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("add_wljsd")
    public void addWljsd() {
        try {
            this.wljsdService.saveWljsd(wljsd);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加物流结算单成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     updateWljsd
     * 功能描述:      修改物流结算单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("update_wljsd")
    public void updateWljsd() {
        try {
            this.wljsdService.updateWljsd(wljsd);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改物流结算单成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     deleteWljsd
     * 功能描述:      删除数据库物流结算单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("delete_wljsd")
    public void deleteWljsd() {
        try {
            this.wljsdService.deleteWljsd(wljsdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除物流结算单成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //修改物流结算单状态
    @Action("change_status")
    public void changeStatus() {
        try {
            wljsdService.changeStatus(wljsdId,zt);
            Jsd jsd = wljsdService.findWljsdDetail(wljsdId);
            dxpzService.sendMsgForWljs(jsd.getJsdh(),jsd.getSqr().getXm(),jsd.getFpze().toString());
            renderText("{\"statusCode\":\"200\", \"message\":\"物流结算单已提交！\", \"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //物流结算单中表格
    @Action("wljsd_table")
    public void wljsdTable() {
        Gson gson = new Gson();
        List<Map> wljsdList=new ArrayList<Map>();
        Jsd jsd = this.wljsdDao.get(wljsdId);
        if (jsd.getShzt() == 1) {
            wljsdList = this.wljsdmxDao.findWljsdDetail(wljsdId);
        } else
            wljsdList = this.wljsdDao.findWljsdDetail(wljsdId);
        Double jfsl = 0d;
        Double je = 0d;
        Double hdsl = 0d;
        Double hdje = 0d;
        Double kdse = 0d;
        Double se = 0d;
        Double me = 0d;
        Double ce = 0d;
        Double jsje = 0d;

        String result = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        java.text.DecimalFormat df2 = new java.text.DecimalFormat("0.0000");
        StringBuffer sbTable = new StringBuffer();
        if (null != wljsdList) {
            sbTable.append("<table class=\"tb intb\">");
            sbTable.append("<tr>");
            sbTable.append("<td >费用类别</td><td>计费数量</td><td>单价(元)</td><td>总金额(元)</td><td>核定数量</td>" +
                    "<td>核定单价(元)</td><td>核定总金额(元)</td><td>面额(元)</td><td>可抵税额(元)</td><td>税额(元)</td><td>净余额(元)</td><td>差额(元)</td><td>备注</td>");
            sbTable.append("</tr>");
            for (int i = 0; i < wljsdList.size(); i++) {
                sbTable.append("<tr>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("fylb.fykmjc")) {
                    sbTable.append(wljsdList.get(i).get("fylb.fykmjc"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("jfsl")) {
                    sbTable.append(df2.format(wljsdList.get(i).get("jfsl")));
                    jfsl += (Double) (wljsdList.get(i).get("jfsl"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("dj")) {
                    sbTable.append(df2.format(wljsdList.get(i).get("dj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("je")) {
                    sbTable.append(df.format(wljsdList.get(i).get("je")));
                    je += (Double) (wljsdList.get(i).get("je"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("hdsl")) {
                    sbTable.append(df2.format(wljsdList.get(i).get("hdsl")));
                    hdsl += (Double) (wljsdList.get(i).get("hdsl"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("hddj")) {
                    sbTable.append(df2.format(wljsdList.get(i).get("hddj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("hdje")) {
                    sbTable.append(df.format(wljsdList.get(i).get("hdje")));
                    hdje += (Double) (wljsdList.get(i).get("hdje"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("me")) {
                    sbTable.append(df.format(wljsdList.get(i).get("me")));
                    me += (Double) (wljsdList.get(i).get("me"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("kdse")) {
                    sbTable.append(df.format(wljsdList.get(i).get("kdse")));
                    kdse += (Double) (wljsdList.get(i).get("kdse"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("se")) {
                    sbTable.append(df.format(wljsdList.get(i).get("se")));
                    se += (Double) (wljsdList.get(i).get("se"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("jsje")) {
                    sbTable.append(df.format(wljsdList.get(i).get("jsje")));
                    jsje += (Double) (wljsdList.get(i).get("jsje"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("ce")) {
                    sbTable.append(df.format(wljsdList.get(i).get("ce")));
                    ce += (Double) (wljsdList.get(i).get("ce"));
                }
                sbTable.append("</td>");


                sbTable.append("<td>");
                if (null != wljsdList.get(i).get("bz")) {
                    sbTable.append(wljsdList.get(i).get("bz"));
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th>合计：");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(df2.format(jfsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(je));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df2.format(hdsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(hdje));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(me));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(kdse));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(se));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(jsje));
            sbTable.append("</td>");


            sbTable.append("<td>");
            sbTable.append(df.format(ce));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</table>");
        }
        renderHtml(sbTable.toString());
    }

    @Action("wld_search")
    public String wldSearch() {
        return SUCCESS;
    }

    //=====================getter and setter================
    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getJsdh() {
        return jsdh;
    }

    public void setJsdh(String jsdh) {
        this.jsdh = jsdh;
    }

    public Long getGlqyId() {
        return glqyId;
    }

    public void setGlqyId(Long glqyId) {
        this.glqyId = glqyId;
    }

    public Long getFpzlId() {
        return fpzlId;
    }

    public void setFpzlId(Long fpzlId) {
        this.fpzlId = fpzlId;
    }

    public String getGlqyQc() {
        return glqyQc;
    }

    public void setGlqyQc(String glqyQc) {
        this.glqyQc = glqyQc;
    }

    public String getGsJc() {
        return gsJc;
    }

    public void setGsJc(String gsJc) {
        this.gsJc = gsJc;
    }

    public Date getSqrqStart() {
        return sqrqStart;
    }

    public void setSqrqStart(Date sqrqStart) {
        this.sqrqStart = sqrqStart;
    }

    public Date getSqrqEnd() {
        return sqrqEnd;
    }

    public void setSqrqEnd(Date sqrqEnd) {
        this.sqrqEnd = sqrqEnd;
    }

    public Jsd getWljsd() {
        return wljsd;
    }

    public void setWljsd(Jsd wljsd) {
        this.wljsd = wljsd;
    }

    public Long getWljsdId() {
        return wljsdId;
    }

    public void setWljsdId(Long wljsdId) {
        this.wljsdId = wljsdId;
    }

    public Integer getWljsdZt() {
        return wljsdZt;
    }

    public void setWljsdZt(Integer wljsdZt) {
        this.wljsdZt = wljsdZt;
    }

    public Long getSprId() {
        return sprId;
    }

    public void setSprId(Long sprId) {
        this.sprId = sprId;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public Long getSydwId() {
        return sydwId;
    }

    public void setSydwId(Long sydwId) {
        this.sydwId = sydwId;
    }

    public String getGldh() {
        return gldh;
    }

    public void setGldh(String gldh) {
        this.gldh = gldh;
    }

    public Long getSydwGsId() {
        return sydwGsId;
    }

    public void setSydwGsId(Long sydwGsId) {
        this.sydwGsId = sydwGsId;
    }

    public Date getCurrentTime() {
        return CurrentTime;
    }

    public void setCurrentTime(Date currentTime) {
        CurrentTime = currentTime;
    }

    public String getSydwqc() {
        return sydwqc;
    }

    public void setSydwqc(String sydwqc) {
        this.sydwqc = sydwqc;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setWljsdService(WljsdService wljsdService) {
        this.wljsdService = wljsdService;
    }
    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }
    @Autowired
    public void setDxpzService(DxpzService dxpzService) {
        this.dxpzService = dxpzService;
    }
    @Autowired
    public void setWljsdDao(WljsdDao wljsdDao) {
        this.wljsdDao = wljsdDao;
    }

    @Autowired
    public void setWljsdmxDao(WljsdmxDao wljsdmxDao) {
        this.wljsdmxDao = wljsdmxDao;
    }
}
